package com.core.zyyz.amc.modules.controller;

import java.text.DecimalFormat;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.core.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcPrjRegisterB;
import com.core.zyyz.amc.modules.service.AmcPrjRegisterBService;
import com.core.common.utils.poi.ExcelUtil;
import com.core.common.core.page.TableDataInfo;

/**
 * 融资项目库登记Controller
 *
 * @author lfs
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/amc/projectregister")
public class AmcPrjRegisterBController extends BaseController
{
    @Autowired
    private AmcPrjRegisterBService amcPrjRegisterBService;

    /**
     * 查询项目库登记列表
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcPrjRegisterB amcPrjRegisterB){
        startPage();
        //attributeText30 该字段目前用来判断前端传值问题
        // 1代表：融资-银行贷款贷款  type_details_code in ('0701','0704','0706')
        // 2代表：融资-债券及非标 type_details_code in ('1402','1403','1405','1499')

        List<AmcPrjRegisterB> list = amcPrjRegisterBService.selectAmcPrjRegisterBList(amcPrjRegisterB);
        if (list != null){
            for(int i = 0 ; i < list.size();i++){
                if (list.get(i).getTotalAssets() != null){
                    String temp = (String)list.get(i).getTotalAssets();
                    temp = temp.replace("[", "");
                    temp = temp.replace("]", "");
                    temp = temp.replace(",","-");
                    //添加千分位
                    temp = addThusand(temp);
                    list.get(i).setTotalAssets(temp);
                }
            }
        }
        return getDataTable(list);
    }


    /**
     * 导出项目库登记列表
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:export')")
    @Log(title = "项目库登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmcPrjRegisterB amcPrjRegisterB)
    {
        List<AmcPrjRegisterB> list = amcPrjRegisterBService.selectAmcPrjRegisterBList(amcPrjRegisterB);
        ExcelUtil<AmcPrjRegisterB> util = new ExcelUtil<AmcPrjRegisterB>(AmcPrjRegisterB.class);
        util.exportExcel(response, list, "项目库登记数据");
    }

    /**
     * 获取项目库登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:query')")
    @GetMapping(value = "/{registerId}")
    public AjaxResult getInfo(@PathVariable("registerId") String registerId)
    {
        return success(amcPrjRegisterBService.selectAmcPrjRegisterBByRegisterId(registerId));
    }

    /**
     * 新增项目库登记
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:add')")
    @Log(title = "项目库登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmcPrjRegisterB amcPrjRegisterB)
    {
        return toAjax(amcPrjRegisterBService.insertAmcPrjRegisterB(amcPrjRegisterB));
    }

    /**
     * 修改项目库登记
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:edit')")
    @Log(title = "项目库登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmcPrjRegisterB amcPrjRegisterB)
    {
        return toAjax(amcPrjRegisterBService.updateAmcPrjRegisterB(amcPrjRegisterB));
    }

    /**
     * 删除项目库登记
     */
    @PreAuthorize("@ss.hasPermi('amc:projectregister:remove')")
    @Log(title = "项目库登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{registerIds}")
    public AjaxResult remove(@PathVariable String[] registerIds)
    {
        return toAjax(amcPrjRegisterBService.deleteAmcPrjRegisterBByRegisterIds(registerIds));
    }

    /**
     * 业务规模字段格式化
     * @param num
     * @return
     */
    public String addThusand(String num){
        DecimalFormat df = new DecimalFormat("###,###.##");
        //添加千分位
        String temp1 = num.substring(0, num.indexOf("-"));
        String temp2 = num.substring(num.indexOf("-") + 1);
        Long i1 = 0L;
        Long i2 = 0L;
        if (!temp1.equals("0")){
            i1 = Long.parseLong(temp1);
        }
        if (!temp2.equals("0") && !"\"\"".equals(temp2)){
            i2 = Long.parseLong(temp2);
        }
        num = df.format(i1);
        num += "-"+df.format(i2);
        return num;
    }
}
